//
//  QYPublishCommentsViewController.m
//  QingYue
//
//  Created by Lei_Zhong_Xun on 2017/6/18.
//  Copyright © 2017年 com.lanou.product_A. All rights reserved.
//

#import "QYPublishCommentsViewController.h"
#import "CDPStarEvaluation.h"
#import "QYFeedbackTextView.h"

@interface QYPublishCommentsViewController ()<CDPStarEvaluationDelegate, UITextViewDelegate>
/** 小绿块*/
@property (nonatomic,strong) UILabel* oneGreenLabel;
/** 评语label*/
@property (nonatomic,strong) UILabel* oneLabel;
//评论输入框
@property (nonatomic, strong) QYFeedbackTextView * textView;
/** 小绿块*/
@property (nonatomic,strong) UILabel* secondGreenLabel;
/** 评语label*/
@property (nonatomic,strong) UILabel* secondLabel;
/** 星级评价*/
@property (nonatomic,strong) CDPStarEvaluation* starEvaluation;

/** 自定义保存弹窗*/
@property (nonatomic,strong) DYSaveShotView* ensureShotView;
/** 自定义保存弹窗遮罩层 */
@property (nonatomic,strong) GKCover* saveShopCover;
/** 发表按钮*/
@property (nonatomic,strong) UIButton* completeButton;

@property (nonatomic, copy) NSNumber *score;

@end

@implementation QYPublishCommentsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"发布评论";
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.textView];
    [self.view addSubview:self.completeButton];
    [self.view addSubview:self.oneGreenLabel];
    [self.view addSubview:self.oneLabel];
    [self.view addSubview:self.secondLabel];
    [self.view addSubview:self.secondGreenLabel];
    _starEvaluation = [[CDPStarEvaluation alloc] initWithFrame:CGRectMake(50, 250, self.view.bounds.size.width * 0.625, self.view.bounds.size.height * 0.07042254) onTheView:self.view];
    _starEvaluation.delegate = self;
    
    _score = @0;

}

#pragma mark CDPStarEvaluationDelegate获得实时评价级别
-(void)theCurrentCommentText:(NSNumber *)score{
    _score = score;
    NSLog(@"评价的分数是:%@", score);
}

#pragma mark - 按钮实现方法
- (void)completeButtonAction
{
    [_textView resignFirstResponder];
    GKCover *cover = [GKCover translucentCoverWithTarget:self action:nil];
    cover.alpha = 0.3;
    cover.frame = [UIScreen mainScreen].bounds;
    [self.view.window addSubview:cover];
    self.saveShopCover = cover;
    self.ensureShotView.frame = CGRectMake(30, MSSize_Height / 2 - 150, MSSize_Width - 60, 160);
    [self.view.window addSubview:self.ensureShotView];
    if ([_textView.text length] == 0) {
        _ensureShotView.contentLabel.text = @"评论失败, 请输入正确内容";
    } else {
        [self publishRequest];
    }
}

- (void)publishRequest {
    [SVProgressHUD show];
    NSDictionary *param = @{@"UserName":[kUserDefaults objectForKey:@"username"],
                            @"Isbn":_isbn,
                            @"Score":_score,
                            @"CommentCont":_textView.text};
    
    [[YCRequestDataService sharedManager] Post:@"AddComment" parameters:param success:^(id response) {
        if ([response[@"Code"] isEqualToString:@"成功"]) {
            [SVProgressHUD dismissWithDelay:0.35];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                _ensureShotView.contentLabel.text = @"评论成功";
            });
        } else {
            kTimeAfter(1, ^{kShowNet(response[@"Exception"]);});
        }
    } failure:^{
        kTimeAfter(1, ^{kShowNet(@"网络或服务器错误");});
    }];
}


/**
 *  隐藏保存按钮提示框
 */
- (void)hiddenSaveShotViewClick
{
    if ([_ensureShotView.contentLabel.text isEqualToString:@"评论成功"]) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
    
    [_saveShopCover removeFromSuperview];
    [_ensureShotView removeFromSuperview];
}
#pragma mark - 布局
- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(8);
        make.right.equalTo(self.view.mas_right).offset(- 8);
        make.top.equalTo(self.view.mas_top).offset(45);
        make.height.equalTo(@(160));
    }];
    [_oneGreenLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_textView.mas_left);
        make.width.equalTo(@4);
        make.top.equalTo(self.view.mas_top).offset(20);
        make.height.equalTo(@15);
    }];
    [_oneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_oneGreenLabel.mas_right).offset(10);
        make.width.equalTo(@30);
        make.centerY.equalTo(_oneGreenLabel.mas_centerY);
        make.height.equalTo(@15);
    }];
    [_secondGreenLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_textView.mas_left);
        make.width.equalTo(@4);
        make.top.equalTo(_textView.mas_bottom).offset(20);
        make.height.equalTo(@15);
    }];
    [_secondLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_oneGreenLabel.mas_right).offset(10);
        make.width.equalTo(@30);
        make.centerY.equalTo(_secondGreenLabel.mas_centerY);
        make.height.equalTo(@15);
    }];
    [_completeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.bottom.equalTo(self.view.mas_bottom);
        make.height.equalTo(@50);
    }];
}

#pragma mark - 点击空白处取消编辑状态
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_textView resignFirstResponder];
}

#pragma mark - lazy load
-(QYFeedbackTextView *)textView{
    
    if (!_textView) {
        _textView = [[QYFeedbackTextView alloc] init];
        _textView.backgroundColor = [UIColor whiteColor];
        _textView.delegate = self;
        _textView.font = [UIFont systemFontOfSize:14.f];
        _textView.textColor = [UIColor blackColor];
        _textView.textAlignment = NSTextAlignmentLeft;
        _textView.editable = YES;
        _textView.layer.cornerRadius = 4.0f;
        _textView.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _textView.layer.borderWidth = 0.5;
        _textView.placeholderColor = [UIColor lightGrayColor];
        _textView.placeholder = @"字数限制140字, 敏感词汇不可提交";
    }
    return _textView;
}

- (UIButton *)completeButton {
    if (!_completeButton) {
        _completeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _completeButton.backgroundColor = RGB(38, 203, 100, 1.0);
        _completeButton.enabled = YES;
        [_completeButton setTitle:@"发表" forState:UIControlStateNormal];
        [_completeButton addTarget:self action:@selector(completeButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _completeButton;
}
- (DYSaveShotView *)ensureShotView
{
    if (!_ensureShotView) {
        _ensureShotView = [[DYSaveShotView alloc] init];
        _ensureShotView.layer.cornerRadius = 11.f;
        [_ensureShotView.sureButton addTarget:self action:@selector(hiddenSaveShotViewClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _ensureShotView;
}

- (CDPStarEvaluation *)starEvaluation
{
    if (!_starEvaluation) {
            }
    return _starEvaluation;
}
- (UILabel *)oneLabel
{
    if (!_oneLabel) {
        _oneLabel = [[UILabel alloc] init];
        _oneLabel.font = Font(14);
        _oneLabel.text = @"评语";
    }
    return _oneLabel;
}

- (UILabel *)oneGreenLabel
{
    if (!_oneGreenLabel) {
        _oneGreenLabel = [[UILabel alloc] init];
        _oneGreenLabel.backgroundColor = RGB(38, 203, 100, 1.0);
    }
    return _oneGreenLabel;
}
- (UILabel *)secondLabel
{
    if (!_secondLabel) {
        _secondLabel = [[UILabel alloc] init];
        _secondLabel.font = Font(14);
        _secondLabel.text = @"评分";
    }
    return _secondLabel;
}

- (UILabel *)secondGreenLabel
{
    if (!_secondGreenLabel) {
        _secondGreenLabel = [[UILabel alloc] init];
        _secondGreenLabel.backgroundColor = RGB(38, 203, 100, 1.0);
    }
    return _secondGreenLabel;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
